<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
  <meta name="renderer" content="webkit" />
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" />
  <title>千峰教育大前端Vue教学</title>
  <!-- 引入vue -->
  <script src="https://cdn.staticfile.org/vue/3.0.5/vue.global.js"></script>
</head>

<body style="margin:100px;font-size:22px;">
  <!-- 定义一个容器 -->
  <div id="app">
    <!-- 声明模板内容 -->
    <div>
      <div>我姓{{firstName}}，叫{{lastName}}，全名：{{fullName}}</div>
      <button @click="resetName">见到白眉鹰王</button>
    </div>
  </div>
  <script>
    const { createApp, computed, ref } = Vue;
    const app = createApp({
      setup() {
        const firstName = ref('曾'); // 姓
        const lastName = ref('阿牛'); // 名
        // const fullName = computed(() => firstName.value + lastName.value); // 全名(只读的情况)
        let fullName = computed({ // 全名(可写的情况)
          get: () => firstName.value + lastName.value,
          set: (fullName) => {
            firstName.value = fullName[0];
            lastName.value = fullName.substr(1);
          },
        }); 
        return { // 返回值会暴露给模板，模板可以直接使用
          firstName,
          lastName,
          fullName,
          resetName: ()=>{ fullName.value = '张无忌' },
        };
      },
    });
    // 挂载应用
    app.mount('#app');
  </script>
</body>

</html>